
console.log('Demo Run JS.[inject]..');

if (window.location.port == '8001') {
    // ...
    if (window.location.pathname == '/LMS/coursePlayer/player.do') {
        
        console.log('准备监听通知消息');
        
        window.addEventListener('message', function(event) { 
            
            console.log('收到通知消息. ', event);
            
            autoPlay();
            
        });
        
        console.log('已经监听通知消息');
    }
}

function autoPlay(){
    var as = $('#v_course a');
    for (var i = 0; i < as.length; i++) {
        if (as[i].className == 'course_chapter do') {
            var key = i + 1;
            $(as[key]).click();
            break;
        }
    }
}

if (window.location.port == '8089') {
    // ...
    if (window.location.pathname == '/coursePlayer/player.do') {
        
        checkVideo();
        
    }
}

function checkVideo(){
    
    if ($('video').length != 0) {
        
        var video = $('video')[0];
        
        console.log('已经获取到Video', video.duration, video);
        
        if (isNaN(video.duration)) {
            setTimeout(function(){
                checkVideo();
            }, 3000);
        } else {

            var time = (video.duration * 1000);
            
            setTimeout(function(){
                
                console.log('top.postMessage()...');
                
                top.postMessage('hello,autoPlay().', '*');
                
            }, time);
            
            console.log('已经设置setTimeout回调. 时间='+time);
        }
    } else {
        setTimeout(function(){
            checkVideo();
        }, 3000);
    }
}
